import Vue from 'vue'
import Vuex from 'vuex'
import { getUserApi } from '../api/my'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 用户信息
    userInfo: {}
  },
  mutations: {
    setUserInfo (state, payload) {
      state.userInfo = {
        ...state.userInfo,
        ...payload
      }
    },
    // 重置vuex
    resetUserInfo (state, payload) {
      state.userInfo = {}
    }
  },
  actions: {
    // 登陆后获取用户信息
    async setUserInfo (context, payload) {
      const res = await getUserApi()
      if (res.message === 'OK') {
        // 保存数据vuex
        context.commit('setUserInfo', res.data)
      }
    }
  },
  modules: {}
})
